import json

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from user import models,userser
from utils.authentication import JWTAuthenticationss
from utils.toojwt import tooljwt
from utils.SendMes import send_message
from utils.myredis import myredis
import random,re

import os
from dashscope import Generation






# Create your views here.
class UserAdd(APIView):
    authentication_classes = [JWTAuthenticationss]
    def post(self,request):
        data = request.data
        user = userser.UserSer(data=data)
        if user.is_valid():
            user.save()
            return Response({'code': 200, 'msg': '注册成功'})
        else:
            return Response({'code': 400, 'msg': user.errors})

class UserLogin(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        data = models.UserModel.objects.filter(username=username).first()
        if data:
            if data.password == password:
                token = tooljwt.gettoken({"id": data.id, "username": data.username,'headimg':data.headimg})
                return Response({'mags':'ok','data':{'id':data.id,'username':data.username,'token':token}})
            else:
                return Response({'code': 400, 'msg': '密码错误'})
        else:
            return Response({'code': 400, 'msg': '用户不存在'})

class UserCenter(APIView):
  9
  def get(self,request):
    phone = request.query_params.get('phone')
    user = models.UserModel.objects.filter(phone=phone).first()
    if user:
      return Response({'code': 400, 'msg': '手机号已存在'})
    else:
        return Response({"msg":'没有手机号，可以注册该手机号',"code":200})

class UserPhoneCode(APIView):
    authentication_classes = [JWTAuthenticationss]
    def get(self,request):
        phone = request.query_params.get('phone')
        if not re.match(r'^1[3-9]\d{9}$', phone):
            return Response({'code': 400, 'msg': '手机号格式错误'})
        else:
            code = str(random.randint(1000, 9999))
            data = json.loads(send_message(phone, code,10))
            print(data)
            if data['statusCode'] == '000000':
                myredis.set_time_str(phone, 60 * 10,code)
                return Response({'code': 200, 'msg': '验证码发送成功'})
            else:
                return Response({'code': 400, 'msg':data['statusCode']})

class UserCode(APIView):
    def post(self,request):
        phone = request.data.get('phone')
        code = request.data.get('code')
        mycode = myredis.get_str(phone)
        data = models.UserModel.objects.filter(phone=phone).first()
        if data:
            if code == mycode:
                token = tooljwt.gettoken({'id':data.id,"username": data.username,'headimg':data.headimg})
                return Response({'mags': '验证码正确，登录成功', 'data': {'id': data.id,'token': token,'code': 200}})
            else:
                return Response({'code': 400, 'msg': '验证码错误,登录失败'})
        else:
            myredis.set_time_str(phone, 60 * 10, 1234)
            if code == mycode:
                datas = models.UserModel.objects.create(phone=phone,username=phone,password='123456',email=phone+'@163.com')
                token = tooljwt.gettoken({'id': datas.id,"username": data.username,'headimg':data.headimg})
                return Response({'mags': '验证码正确，注册成功', 'data': {'id': datas.id,'token': token}})
            else:
                return Response({'code': 400, 'msg': '验证码错误,注册失败'})

class UserImg(APIView):
    authentication_classes = [JWTAuthenticationss]
    def get(self,request):
        id = request.query_params.get('id')
        userlist = models.UserImg.objects.filter(id=id).first()
        ser = userser.UserimgSer(userlist)

        return Response({'msg':'ok','data':ser.data})

class UserCenterss(APIView):
  authentication_classes = [JWTAuthenticationss]
  def get(self,request):
    return Response({"msg":'你好'})














